import * as RECEIVE_DOC_APIS from '../apis/receiveDoc';
import * as SEND_DOC_APIS from '../apis/sendDoc';
// import AlertModal from '../public/AlertModal';
import { message } from 'antd';
import { WeaLocaleProvider } from 'ecCom';

const getLabel = WeaLocaleProvider.getLabel;
export function getSelectedRowKeys(tableStore) {
  const selectedRowKeys = tableStore.selectedRowKeys;
  return selectedRowKeys.join(',');
}

// 显示列定制按钮的ev handler
export function onColSetBtnClick(compStore, tableStore) {
  const dataKey = compStore.dataKey;

  tableStore.setColSetVisible(dataKey, true);
  tableStore.tableColSet(dataKey, true);
}

// 搜索按钮的ev handler
export function onSearchBtnClick(compStore, viewconditiondomkey = '') {
  if (compStore.pageName === 'collectionSend' || compStore.pageName === 'collectionReceive') {
    // 因为除了weaform参数，还需要多穿一个vc给后台，所以有了otherInterfaceParams
    compStore.doSearch({
      ifAdFormCgWhenCgTab: false,
      behavior: 'search',
      otherInterfaceParams: {
        viewcondition: compStore.viewcondition,
      },
    });
  } else if (compStore.pageName === 'receiveDoc') {
    let otherInterfaceParams = {
      menuType: compStore.menuType,
      isInnerChange: compStore.isInnerChange,
      state: compStore.viewcondition,
    };
    if (compStore.menuType == 'receiptRegistration') {
      otherInterfaceParams = {
        menuType: 'receiptRegistration',
      };
    }
    compStore.doSearch({
      ifAdFormCgWhenCgTab: true,
      behavior: 'search',
      viewconditiondomkey,
      otherInterfaceParams,
    });
  } else {
    compStore.doSearch({
      ifAdFormCgWhenCgTab: true,
      behavior: 'search',
      viewconditiondomkey,
      otherInterfaceParams: {
        menuType: compStore.menuType,
        isInnerChange: compStore.isInnerChange,
      },
    });
  }
  compStore.setStoreAttr('ifShowSearchAd', false);
}

// 接收公文 处理方法 共有的操作
const getParams = (store) => {
  store.setStoreAttr('loading', true);
  const ids = store.selectRowId.join(',');
  const params = { ids };
  return params;
};

// 接收公文 处理方法 调用成功后 共用的方法
const callBack = (data, store) => {
  const { api_status, api_message } = data;
  if (api_status) {
    if (api_message == '') {
      message.success(getLabel('30700', '操作成功'));
    } else {
      message.success(api_message);
    }
  } else {
    message.error(api_message);
  }
  store.setStoreAttr('loading', false);
  store.selectRowId = [];
  store.doSearch({
    ifAdFormCgWhenCgTab: true,
    behavior: 'search',
    viewconditiondomkey: 'state',
    otherInterfaceParams: {
      menuType: store.menuType,
      isInnerChange: store.isInnerChange,
    },
  });
};

// 接收公文->接收按钮的ev handler
export function onreceiveDocBtnClick(compStore) {
  compStore.loading = true;
  const params = { isInnerChange: compStore.isInnerChange };
  RECEIVE_DOC_APIS.receiveDoc(params).then((data) => {
    callBack(data, compStore);
  });
}

// 接收公文->签收按钮的ev handler
export function onSigninDocBtnClick(compStore, status) {
  let params = {};
  Object.assign(params, getParams(compStore), { status }, { isInnerChange: compStore.isInnerChange });
  RECEIVE_DOC_APIS.signinDoc(params).then((data) => {
    callBack(data, compStore);
  });
}

// 接收公文->手动签收按钮的一些功能
export function onManualSigninBtnClick(compStore, status) {
  const reason = compStore.manualSigninForm.getFormParams();
  const ids = getParams(compStore);
  let params = {};
  Object.assign(params, reason, ids, { status }, { isInnerChange: compStore.isInnerChange });
  RECEIVE_DOC_APIS.manualSigninDoc(params).then((data) => {
    callBack(data, compStore);
    compStore.setStoreAttr('ifShowManualSigninForm', false);
  });
}
// 接收公文->智能签收按钮的一些功能
export function onIntelligentSigninBtnClick(compStore, status) {
  const reason = compStore.intelligentSigninForm.getFormParams();
  const ids = getParams(compStore);
  let params = {};
  Object.assign(params, reason, ids, { status }, { isInnerChange: compStore.isInnerChange });
  RECEIVE_DOC_APIS.intelligentSigninDoc(params).then((data) => {
    callBack(data, compStore);
    compStore.setStoreAttr('ifShowIntelligentSigninForm', false);
  });
}
// 接收公文->拒收按钮的一些功能
// whichBtnClicked为0时表示拒收按钮的功能逻辑，为1表示填写了拒收理由后点确认时的功能逻辑
export function onRejectDocBtnClick(compStore, status) {
  const reason = compStore.rejectForm.getFormParams();
  const ids = getParams(compStore);
  let params = {};
  Object.assign(params, reason, ids, { status }, { isInnerChange: compStore.isInnerChange });
  RECEIVE_DOC_APIS.rejectDoc(params).then((data) => {
    callBack(data, compStore);
    compStore.setStoreAttr('ifShowRejectForm', false);
  });
}

// 接收公文->退回按钮的ev handler
export function onSendbackBtnClick(compStore, status) {
  const reason = compStore.sendBackForm.getFormParams();
  const ids = getParams(compStore);
  let params = {};
  Object.assign(params, reason, ids, { status }, { isInnerChange: compStore.isInnerChange });
  RECEIVE_DOC_APIS.sendbackDoc(params).then((data) => {
    callBack(data, compStore);
    compStore.setStoreAttr('ifSendBackForm', false);
  });
}

// 接收公文->创建流程按钮的ev handler
export function onCreateWorkflowBtnClick(compStore) {
  // const params = {
  //     ids: compStore.selTableIdOfcreateWF,
  //     sn: compStore.createWFsn,
  //     createWfid: compStore.createWfid,
  //     isInnerChange: compStore.isInnerChange
  // }
  // RECEIVE_DOC_APIS.createworkflow(params).then(data => {
  //     const { status } = data;
  //     if (status) {
  //         AlertModal('success', data.message);
  //         window.open(data.url);
  //     } else {
  //         AlertModal('warning', '流程创建失败');
  //     }
  // });
  compStore.showCreatFlow = true;
}

// 发送公文-> 发送 操作
export function onsendDocBtnClick(compStore) {
  compStore.loading = true;
  const params = {
    ids: compStore.selectRowId.join(','),
    status: 1,
    isInnerChange: compStore.isInnerChange,
  };
  SEND_DOC_APIS.sendDoc(params).then((data) => {
    callBack(data, compStore);
  });
}
/* ----------------- 收文登记 --------------------------*/

// 收文登记-> 电子登记
export function eleRegist(compStore) {
  compStore.showRegistDialog();
}

// 收文登记-> 批量删除
export function batchDeleteList(compStore) {
  compStore.delList(compStore.selectRowId.join(','));
}

